$(function(){
	var t = $("#nodeTableList").bootstrapTable({
        url: constUrl + '/conf/hosts',
        method: 'get',
        dataType: "json",
        striped: true,//设置为 true 会有隔行变色效果
        undefinedText: "",//当数据为 undefined 时显示的字符
        pagination: true, //分页
        paginationLoop:true,//设置为 true 启用分页条无限循环的功能。
        pageNumber: 1,//如果设置了分页，首页页码
        sortable:true,
        sortorder:"asc",
        pageSize: 10,//如果设置了分页，页面数据条数
        pageList: [ 10, 20, 50],	//如果设置了分页，设置可供选择的页面数据条数。设置为All 则显示所有记录。
        paginationPreText: '‹',//指定分页条中上一页按钮的图标或文字,这里是<
        paginationNextText: '›',//指定分页条中下一页按钮的图标或文字,这里是>
        search: true, //显示搜索框
        data_local: "zh-CN",//表格汉化
        sidePagination: "server", //服务端处理分页
        queryParams: function (params) {//自定义参数，这里的参数是传给后台的，我这是是分页用的
            return {//这里的params是table提供的
                offset:params.offset,///currentPage: params.offset/params.limit,//从数据库第几条记录开始
                pageSize: params.limit,//找多少条
                searchText: params.search,
                sort:params.sort,
                sortOrder:params.order
            };
           },
        responseHandler:function(res){
        		return res.data;
           },
        idField: "nodeId",//指定主键列
        columns: [
        	{
            title: '节点ID',//表的列名
            field: 'nodeId',//json数据中rows数组中的属性名
            align: 'center'//,//水平居中
        	},
            {
                title: '节点名称',//表的列名
                field: 'nodeName',//json数据中rows数组中的属性名
                sortable:true,
                align: 'center'//水平居中
            },
            {
                title: '节点IP',
                field: 'nodeIp',
                sortable:true,
                align: 'center'
            },
            {
                title: '状态',
                field: 'status',
                sortable:true,
                align: 'center'
            },
            {
                //EMAIL
                title: 'Gpu标识',
                field: 'gpuIdentify',
                sortable:true,
                align: 'center'
            },
            {
                title: '节点分组',
                field: 'groupName',
                sortable:true,
                align: 'center'
            },
            {
                title: '操作',
                field: '',
                align: 'center',
                formatter:function(value,row,index){
                	 var trTemp="<div class=\"am-btn-group am-btn-group-xs\">" ;
                  if(isNodeCanOper(row.status)){ trTemp+="<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"nodeOper('"+row.nodeName+"','"+ row.status+"','"+row.nodeId+"')\">"+ ((row.status=="ok")?"下线":"上线")+
                     "</div>" ;
                     	}
                 trTemp+=
                 "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"openNodeDetail('"+row.nodeName+"')\">详情</div>" +
                 "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"nodeRemove('"+row.nodeName+"')\">删除</div>" +
                 "</div>";
                 return trTemp;
                }
            }
 
        ]
    }); 
    t.on('load-success.bs.table', function (data) {//table加载成功后的监听函数
        console.log("load success");
        $(".pull-right").css("display", "block");
    }); 
	
});
function nodeStatus(status){
	switch(status){
		case "closed_Adm":return "已下线";break;
		case "closed_Busy":return "节点忙";break;
		case "closed_Full":return "作业已满";break;
		case "closed_Lim":return "LIM不可达";break;
		case "closed_Lock":return "节点被锁定";break;
		case "closed_Wind":return "被调度关闭";break;
		case "ok":return "正常";	break;
		default:return "未知状态（"+status+"）";;break;
	}	
}
function isNodeCanOper(status){
	switch(status){
		case "closed_Adm"://return "已下线";break;
		case "closed_Busy"://return "节点忙";break;
		case "closed_Full"://return "作业已满";break;
		case "closed_Lock"://return "节点被锁定";break;
		case "closed_Wind"://return "被调度关闭";break;
		case "ok":return true;	break;
		default:return false;break;
	}	
}
function openNodeDetail(id) {
    window.location.href=constUrl+"views/node/detail.action?nodeId="+id;
}
function editNodeDetail(id) {
    window.location.href=constUrl+"views/node/update.action?nodeId="+id;
}
function nodeOper(data){
	nodeOper(data.nodeName,data.status=="open"?"close":"open",data.nodeId)
}
function nodeOper(nodeName,oper,id) {
    var index = parent.layer.getFrameIndex(window.name);
    //alert(oper)
    switch(oper){
		case "closed_Adm":oper= "open";break;//return "已下线";break;
		case "closed_Busy"://return "节点忙";break;
		case "closed_Full"://return "作业已满";break;
		case "closed_Lock"://return "节点被锁定";break;
		case "closed_Wind"://return "被调度关闭";break;
		case "ok":oper= "close";	break;
	}
    //alert(oper)
    //alert((oper=="close"?"下线":"上线"))
    layer.confirm('确定要'+ (oper=="close"?"下线":"上线")+'吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type:"POST",
            url:constUrl+"conf/hosts/"+nodeName+"/"+oper+"/"+id+".action",
            dataType:"json",            
            success:function(data){
            	if(data.resultCode=='000000'){
	            	//alert(data.resultCode);
	                layer.confirm('操作成功！',{
	                    btn:['确定']
	                },function () {
	                    window.location.reload(index);//下线内容已变更，页面需要重新加载
		                })
	             }else
	            	 layer.msg('操作失败')
            },
            error:function(responseText) {
                layer.msg('操作失败')
            },
        })
    }, function(){
        layer.msg('操作取消', {
            time: 1000 //1s后自动关闭
        });
    });
}
function nodeRemove(nodeName) {
    var index = parent.layer.getFrameIndex(window.name);
    layer.confirm('删除后节点将无法恢复，确定要删除吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type:"DELETE",
            url:constUrl+"conf/hosts/"+nodeName+".action",
            dataType:"json",            
            success:function(data){
            	if(data.resultCode=='000000'){
	                layer.confirm(data.desc,{
	                    btn:['确定']
	                },function () {
	                    parent.location.reload(index);//删除后页面内容已变更，需要重新加载
		                })
	             }else
	            	 layer.msg(data.desc);
            },
            error:function(responseText) {
                layer.msg('操作失败')
            },
        })
    }, function(){
        layer.msg('操作取消', {
            time: 1000 //1s后自动关闭
        });
    });
}
